//用户管理
#include "tab4.h"

// 用户管理相关变量
static lv_obj_t *user_table = NULL;
static lv_obj_t *user_detail_modal = NULL;
static lv_obj_t *user_orders_table = NULL;
static lv_obj_t *delete_confirm_modal = NULL;
static User *user_to_delete = NULL;


static void refresh_user_table(void);
// 释放用户内存
static void free_user(User *user) 
{
    if (!user) return;
    
    if (user->orders) 
    {
        free(user->orders);
    }
    free(user);
}

// 从链表中删除用户
static int delete_user_from_list(UserList *list, const char *username) 
{
    if (!list || !username) return 0;
    
    User *current = list->head;
    User *prev = NULL;
    printf("用户 %s 已删除\n", user_to_delete->username);

    while (current) 
    {
        if (strcmp(current->username, username) == 0) 
        {
            if (prev == NULL) {
                list->head = current->next;
            } else {
                prev->next = current->next;
            }
            
            free_user(current);
            list->count--;
            return 1;
        }
        
        prev = current;
        current = current->next;
    }
    
    return 0;
}

// 关闭删除确认模态窗口
static void close_delete_confirm_modal_cb(lv_event_t *e) 
{
    if (delete_confirm_modal) 
    {
        lv_obj_del(delete_confirm_modal);
        delete_confirm_modal = NULL;
        user_to_delete = NULL;
    }
}

// 确认删除用户
static void confirm_delete_user_cb(lv_event_t *e) 
{
    if (!user_to_delete) {
        close_delete_confirm_modal_cb(e);
        return;
    }
    
    // 从链表中删除用户
    if (delete_user_from_list(userList, user_to_delete->username)) 
    {
        // printf("用户 %s 已删除\n", user_to_delete->username);
        // 保存到文件
        saveUsersToFile(userList, "userManagement.json");
        
        // 刷新表格
        refresh_user_table();
        
    } 
    else 
    {
        printf("删除用户失败\n");
    }
    
    close_delete_confirm_modal_cb(e);
}

// 显示删除确认对话框
static void show_delete_confirm_dialog(User *user) 
{
    if (!user) return;
    
    user_to_delete = user;
    
    // 创建确认模态窗口
    delete_confirm_modal = lv_obj_create(lv_scr_act());
    lv_obj_set_size(delete_confirm_modal, 350, 200);
    lv_obj_align(delete_confirm_modal, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_opa(delete_confirm_modal, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(delete_confirm_modal, lv_color_hex(0x2D2D3A), 0);
    lv_obj_set_style_border_width(delete_confirm_modal, 2, 0);
    lv_obj_set_style_border_color(delete_confirm_modal, lv_color_white(), 0);
    
    // 确认消息
    lv_obj_t *confirm_label = lv_label_create(delete_confirm_modal);
    char confirm_text[100];
    snprintf(confirm_text, sizeof(confirm_text), "确定要删除用户 %s 吗？", user->username);
    lv_label_set_text(confirm_label, confirm_text);
    lv_obj_set_style_text_font(confirm_label, font_chinese, 0);
    lv_obj_set_style_text_color(confirm_label, lv_color_white(), 0);
    lv_obj_align(confirm_label, LV_ALIGN_TOP_MID, 0, 30);
    
    // 按钮容器
    lv_obj_t *btn_container = lv_obj_create(delete_confirm_modal);
    lv_obj_set_size(btn_container, 300, 50);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, -20);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_bg_opa(btn_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    
    // 确认按钮
    lv_obj_t *confirm_btn = lv_btn_create(btn_container);
    lv_obj_set_size(confirm_btn, 100, 40);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0xF44336), 0); // 红色
    lv_obj_t *confirm_btn_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_btn_label, "确定");
    lv_obj_set_style_text_font(confirm_btn_label, font_chinese, 0);
    lv_obj_center(confirm_btn_label);
    lv_obj_add_event_cb(confirm_btn, confirm_delete_user_cb, LV_EVENT_CLICKED, NULL);
    
    // 取消按钮
    lv_obj_t *cancel_btn = lv_btn_create(btn_container);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0x9E9E9E), 0); // 灰色
    lv_obj_t *cancel_btn_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_btn_label, "取消");
    lv_obj_set_style_text_font(cancel_btn_label, font_chinese, 0);
    lv_obj_center(cancel_btn_label);
    lv_obj_add_event_cb(cancel_btn, close_delete_confirm_modal_cb, LV_EVENT_CLICKED, NULL);
}

// 刷新用户表格
static void refresh_user_table() 
{
    if (!user_table || !userList) return;
    
    // 清除现有内容
    lv_table_set_row_cnt(user_table, 1); // 保留标题行
    lv_table_set_cell_value(user_table, 0, 0, "用户名");
    lv_table_set_cell_value(user_table, 0, 1, "状态");
    lv_table_set_cell_value(user_table, 0, 2, "操作");
    lv_table_set_cell_value(user_table, 0, 3, "删除");
    
    // 添加用户数据
    freeUserList(userList);
    userList = initUserList();
    if (userList) 
    {
        loadUsersFromFile(userList, "userManagement.json");
    }

    User *current = userList->head;
    int row = 1;
    while (current) 
    {
        lv_table_set_row_cnt(user_table, row + 1);
        
        // 用户名
        lv_table_set_cell_value(user_table, row, 0, current->username);
        
        // 状态
        lv_table_set_cell_value(user_table, row, 1, current->state);
        
        // 操作按钮
        lv_table_set_cell_value(user_table, row, 2, strcmp(current->state, "正常") == 0 ? "封禁" : "启用");
        
        // 删除按钮
        lv_table_set_cell_value(user_table, row, 3, "删除");
       
        current = current->next;
        row++;
    }
}

// 关闭详情模态窗口的回调函数
static void close_detail_modal_cb(lv_event_t *e) 
{
    if (user_detail_modal) {
        lv_obj_del(user_detail_modal);
        user_detail_modal = NULL;
        user_orders_table = NULL;
    }
}

// 显示用户详情
static void show_user_detail(User *user) 
{
    if (!user) return;
    
    // 创建模态窗口
    if (user_detail_modal) lv_obj_del(user_detail_modal);
    
    user_detail_modal = lv_obj_create(lv_scr_act());
    lv_obj_set_size(user_detail_modal, 600, 400);
    lv_obj_align(user_detail_modal, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_opa(user_detail_modal, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(user_detail_modal, lv_color_hex(0x2D2D3A), 0);
    lv_obj_set_style_border_width(user_detail_modal, 2, 0);
    lv_obj_set_style_border_color(user_detail_modal, lv_color_white(), 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(user_detail_modal);
    char title_text[100];
    snprintf(title_text, sizeof(title_text), "用户详情: %s", user->username);
    lv_label_set_text(title, title_text);
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 用户信息
    lv_obj_t *info_label = lv_label_create(user_detail_modal);
    char info_text[200];
    snprintf(info_text, sizeof(info_text), "用户名: %s\n密码: %s\n状态: %s", 
             user->username, user->userPassword, user->state);
    lv_label_set_text(info_label, info_text);
    lv_obj_set_style_text_font(info_label, font_chinese, 0);
    lv_obj_set_style_text_color(info_label, lv_color_white(), 0);
    lv_obj_align(info_label, LV_ALIGN_TOP_LEFT, 20, 50);
    
    // 订单标题
    lv_obj_t *orders_title = lv_label_create(user_detail_modal);
    lv_label_set_text(orders_title, "订单列表:");
    lv_obj_set_style_text_font(orders_title, font_chinese, 0);
    lv_obj_set_style_text_color(orders_title, lv_color_white(), 0);
    lv_obj_align(orders_title, LV_ALIGN_TOP_LEFT, 20, 120);
    
    // 订单表格
    user_orders_table = lv_table_create(user_detail_modal);
    lv_obj_set_size(user_orders_table, 560, 200);
    lv_obj_align(user_orders_table, LV_ALIGN_TOP_LEFT, 20, 150);
    lv_obj_set_style_text_font(user_orders_table, font_chinese, 0);
    
    // 设置表格列宽
    lv_table_set_col_width(user_orders_table, 0, 100);
    lv_table_set_col_width(user_orders_table, 1, 100);
    lv_table_set_col_width(user_orders_table, 2, 100);
    lv_table_set_col_width(user_orders_table, 3, 100);
    lv_table_set_col_width(user_orders_table, 4, 80);
    
    // 设置表格标题
    lv_table_set_cell_value(user_orders_table, 0, 0, "车次");
    lv_table_set_cell_value(user_orders_table, 0, 1, "出发站");
    lv_table_set_cell_value(user_orders_table, 0, 2, "到达站");
    lv_table_set_cell_value(user_orders_table, 0, 3, "日期");
    lv_table_set_cell_value(user_orders_table, 0, 4, "座位号");
    
    // 添加订单数据
    for (int i = 0; i < user->orderCount; i++) {
        lv_table_set_cell_value(user_orders_table, i+1, 0, user->orders[i].TrainNumber);
        lv_table_set_cell_value(user_orders_table, i+1, 1, user->orders[i].start);
        lv_table_set_cell_value(user_orders_table, i+1, 2, user->orders[i].end);
        lv_table_set_cell_value(user_orders_table, i+1, 3, user->orders[i].date);
        
        char seat[10];
        snprintf(seat, sizeof(seat), "%d", user->orders[i].seatNumber);
        lv_table_set_cell_value(user_orders_table, i+1, 4, seat);
    }
    
    // 关闭按钮
    lv_obj_t *close_btn = lv_btn_create(user_detail_modal);
    lv_obj_set_size(close_btn, 100, 40);
    lv_obj_align(close_btn, LV_ALIGN_TOP_RIGHT, -20, -20);
    lv_obj_t *close_label = lv_label_create(close_btn);
    lv_label_set_text(close_label, "关闭");
    lv_obj_set_style_text_font(close_label, font_chinese, 0);
    lv_obj_center(close_label);
    lv_obj_add_event_cb(close_btn, close_detail_modal_cb, LV_EVENT_CLICKED, NULL);
}

// 表格点击事件处理
static void user_table_event_cb(lv_event_t *e) 
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_VALUE_CHANGED) 
    {
        uint16_t col, row;
        lv_table_get_selected_cell(target, &row, &col);
        
        if (row == 0) return; // 标题行
        
        // 获取对应用户
        User *current = userList->head;
        for (int i = 1; i < row; i++) 
        {
            if (current) current = current->next;
        }
        
        if (!current) return;
        
        if (col == 0) 
        { // 点击用户名列，显示详情
            show_user_detail(current);
        } 
        else if (col == 2) 
        { // 点击操作列，切换状态
            // 切换状态
            if (strcmp(current->state, "正常") == 0) 
            {
                strcpy(current->state, "封禁");
            } else {
                strcpy(current->state, "正常");
            }
            
            // 保存到文件
            saveUsersToFile(userList, "userManagement.json");
            
            // 刷新表格
            refresh_user_table();
        }
        else if(col == 3)
        {
            // 点击删除列，显示删除确认对话框
            show_delete_confirm_dialog(current);
        }
    }
}

// 刷新按钮点击事件
static void refresh_btn_event_cb(lv_event_t *e) 
{
    refresh_user_table();
}

// 初始化用户管理界面
void init_user_management_tab4(lv_obj_t *parent) 
{
    // 创建容器
    lv_obj_t *container = lv_obj_create(parent);
    lv_obj_set_size(container, lv_pct(100), lv_pct(100));
    lv_obj_set_style_border_width(container, 0, 0);
    lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(container);
    lv_label_set_text(title, "用户管理");
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_LEFT, 20, 10);
    
    // 刷新按钮
    lv_obj_t *refresh_btn = lv_btn_create(container);
    lv_obj_set_size(refresh_btn, 100, 40);
    lv_obj_align(refresh_btn, LV_ALIGN_TOP_RIGHT, -20, 10);
    lv_obj_t *refresh_label = lv_label_create(refresh_btn);
    lv_label_set_text(refresh_label, "刷新");
    lv_obj_set_style_text_font(refresh_label, font_chinese, 0);
    lv_obj_center(refresh_label);
    lv_obj_add_event_cb(refresh_btn, refresh_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 创建用户表格
    user_table = lv_table_create(container);
    lv_obj_set_size(user_table, lv_pct(95), lv_pct(80));
    lv_obj_align(user_table, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_style_text_font(user_table, font_chinese, 0);
    
    // 设置表格列宽
    lv_table_set_col_width(user_table, 0, 150);
    lv_table_set_col_width(user_table, 1, 100);
    lv_table_set_col_width(user_table, 2, 100);
    lv_table_set_col_width(user_table, 3, 100);
    
    // 设置表格标题
    lv_table_set_cell_value(user_table, 0, 0, "用户名");
    lv_table_set_cell_value(user_table, 0, 1, "状态");
    lv_table_set_cell_value(user_table, 0, 2, "操作");
    lv_table_set_cell_value(user_table, 0, 3, "操作");

    // 设置表格事件
    lv_obj_add_event_cb(user_table, user_table_event_cb, LV_EVENT_ALL, NULL);
    
    // 初始加载数据
    refresh_user_table();
}